package com.tiankong.product.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiankong.product.entity.Attr;
import com.tiankong.product.entity.Category;
import com.tiankong.product.enums.Type;
import com.tiankong.product.service.AttrService;
import com.tiankong.product.service.CategoryService;
import net.youdi.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody Attr attr){
        if (attr.getAttrId() ==null){
            attrService.saveAttr(attr);
        }else{
            attrService.updateAttr(attr);
        }
        return Result.ok(attr);
    }

    @GetMapping("/list/{attrType}/{categoryId}")
    public Result baseList(@PathVariable("categoryId") Long categoryId,
                           @PathVariable("attrType") String attrType,
                @RequestParam(value = "key",required = false) String key,
               @RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,
               @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        QueryChainWrapper<Attr> query = attrService.query().eq("attr_type",attrType);
        if (categoryId > 0){
            query.eq("catelog_id",categoryId);
        }
        if (StringUtils.hasText(key)) {
            query.like("attr_name",key);
        }
        Page<Attr> page = query.page(new Page<>(pageIndex, pageSize));
        List<Attr> attrs = page.getRecords();
        if (CollectionUtil.isNotEmpty(attrs)){
            Map<Long, List<Attr>> group = attrs.stream().collect(Collectors.groupingBy(Attr::getCatelogId));
            List<Category> categories = categoryService.listByIds(group.keySet());
            for (Category category : categories) {
                List<Attr> list = group.get(category.getCatId());
                list.forEach(a->a.setCatName(category.getName()));
            }
        }
        return Result.ok().setPage(page.getTotal(),page.getRecords());
    }
    @GetMapping("sale/list/{categoryId}")
    public Result saleList(@PathVariable("categoryId") Long categoryId){
        List<Attr> attrs = attrService.query().eq("attr_type", Type.Attr.SALE.getCode()).eq("catelog_id",categoryId).list();
        return Result.ok(attrs);
    }
    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Long[] attrIds){
        attrService.batchDelete(Arrays.asList(attrIds));
        return Result.ok();
    }

    @GetMapping("/info/{attrId}")
    public Result info(@PathVariable("attrId") Long attrId){
        Attr attr = attrService.getAttrInfo(attrId);

        return Result.ok(attr);
    }
}
